/***************************************************************************************************************
 *   Author : Manu Joseph
 *   Date : 05/05/2010
 *   Description : A tool that easily allow users to share folders and set permissions
 *   File Name : ContactForm.cs
 *   Purpose : Form for adding/removing/editing contacts
****************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EasyShare
{
    /// <summary>
    /// Form for adding/editing a contact
    /// </summary>
    public partial class ContactForm : Form
    {
        #region Private Instance Members

        private bool m_isAdd;                               // indicates whether form is used for adding/editing
        private ContactManager m_contactManager = null;     // contact manager for operations on database

        #endregion

        #region Constructors

        // constructor for add mode
        public ContactForm(ContactManager contactManager)
        {
            InitializeComponent();

            m_contactManager = contactManager;
            
            AddButton.Text = "Add";
            this.Text = "Add Contact";
            m_isAdd = true;
        }

        // constructor for edit mode
        public ContactForm(ContactManager contactManager, string name, string alias)
        {
            InitializeComponent();

            m_contactManager = contactManager;
            NameTextBox.Text = name;
            NameTextBox.ReadOnly = true;
            AliasNameTextBox.Text = alias;

            AddButton.Text = "Save";
            this.Text = "Edit Contact";
            m_isAdd = true;
        }

        #endregion

        // Checks if the contact is a valid contact
        private void CheckNameLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string contactName = NameTextBox.Text.Trim();
            if (contactName == string.Empty)
            {
                MessageBox.Show("Please enter a contact name to check", EasyShareConstants.TOOL_NAME,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.Cursor = Cursors.WaitCursor;

            string[] invalidUsers = null;
            Dictionary<string, string> userMap = null;
            if (Win32ShareWrapper.GetInvalidUsers(new string[] { contactName }, ref invalidUsers, ref userMap) == true)
            {
                // Since we have given only one contact for verification
                // if the return array has atlease one contact then the contact give
                // is invalid
                if (invalidUsers.Length == 0)
                {
                    // setting the full name label
                    if (userMap.ContainsKey(contactName) == true)
                    {
                        FullNameLabel.Text = "(" + userMap[contactName] + ")";
                    }

                    MessageBox.Show("Contact name given is valid", EasyShareConstants.TOOL_NAME,
                        MessageBoxButtons.OK, MessageBoxIcon.Information);                    
                }
                else
                {
                    MessageBox.Show("Contact name given was not found." + Environment.NewLine + Environment.NewLine + " Please enter usernames in the format '<domain_name>\\<user_name>'",
                        EasyShareConstants.TOOL_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("Error while checking for contact name", EasyShareConstants.TOOL_NAME,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Cursor = Cursors.Default;
        }

        // Adds/Edits the entered contact
        private void AddButton_Click(object sender, EventArgs e)
        {
            string contactName = NameTextBox.Text.Trim();
            string aliasName = AliasNameTextBox.Text.Trim();

            // performing validations
            if (contactName == string.Empty)
            {
                MessageBox.Show("Please specify a contact name", EasyShareConstants.TOOL_NAME,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (aliasName == string.Empty)
            {
                MessageBox.Show("Please specify an alias name", EasyShareConstants.TOOL_NAME,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (m_contactManager.SaveContact(contactName, aliasName) == true)
            {
                // save succeeded -- close the form
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("Contact save failed", EasyShareConstants.TOOL_NAME,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Cancelbutton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        // resets the full name label if the name changes
        private void NameTextBox_TextChanged(object sender, EventArgs e)
        {
            FullNameLabel.Text = string.Empty;
        }
    }
}